package com.liaoban.app.ui.message;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.liaoban.app.R;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.ui.MainActivity;
import com.liaoban.app.ui.base.BaseFragment;
import com.netease.nim.demo.session.SessionHelper;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nim.uikit.business.recent.RecentContactsCallback;
import com.netease.nim.uikit.business.recent.RecentContactsFragment;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.RecentContact;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 消息
 */
public class MessageFragment extends BaseFragment {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_content)
    TextView tvContent;

    private RecentContactsCallback mCallback = new RecentContactsCallback() {
        @Override
        public void onRecentContactsLoaded() {

        }

        @Override
        public void onUnreadCountChange(int unreadCount) {
            MainActivity mainActivity = (MainActivity) getActivity();
            mainActivity.setMsgCount(unreadCount);
        }

        @Override
        public void onItemClick(RecentContact recent) {
            if (recent.getSessionType() == SessionTypeEnum.Team) {
                NimUIKit.startTeamSession(getActivity(), recent.getContactId());
            } else if (recent.getSessionType() == SessionTypeEnum.P2P) {
                NimUIKit.startP2PSession(getActivity(), recent.getContactId());
            }
        }

        @Override
        public String getDigestOfAttachment(RecentContact recentContact, MsgAttachment attachment) {
            return null;
        }

        @Override
        public String getDigestOfTipMsg(RecentContact recent) {
            return null;
        }
    };

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initView(View contentView) {
        toolbar.setNavigationOnClickListener(v -> {
            MainActivity mainActivity = (MainActivity) getActivity();
            mainActivity.changeTab(0);
        });//后退键
        tvTitle.setText(R.string.tab_msg);
        ivRight.setImageResource(R.mipmap.ic_home_sound);
        ivRight.setVisibility(View.VISIBLE);
        initRecentContactsFragment();
        loadData();
    }

    private void loadData() {
        showRunningDialog();
        startTask(CommonBiz.getInstance().messageIndex(), messageIndexBaseResponse -> {
            tvContent.setText(messageIndexBaseResponse.data.getContent());
        });
    }

    /**
     * 最近联系人
     */
    private void initRecentContactsFragment() {
        RecentContactsFragment fragment = new RecentContactsFragment();
        FragmentManager fgManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fgManager.beginTransaction();
        fragmentTransaction.replace(R.id.layout_recent_contacts, fragment).commit();
        fragment.setCallback(mCallback);
    }

    @OnClick(R.id.iv_right)
    public void right() {
        startActivity(new Intent(getActivity(), CallHistoryActivity.class));
    }

    @OnClick(R.id.layout_notice)
    public void notice() {
        startActivity(new Intent(getActivity(), NoticeActivity.class));
    }

    @OnClick(R.id.layout_xms)
    public void xms() {
        SessionHelper.startP2PSession(getActivity(), "18888999999");
    }

}
